package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        appointment.setCreateTime(new Date());
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        //需要额外检查是否可能导致数据丢失问题
        //必须是预约中才可以进行编辑
        Appointment oldObj = appointmentMapper.selectByPrimaryKey(appointment.getId());
        if (oldObj ==null || !Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BusinessException("必须预约中才能编辑数据");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public void delete(Long id) {
        appointmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void arrival(Long id) {
        // 必须是预约状态中才能进行用户到店
        Appointment oldObj = appointmentMapper.selectByPrimaryKey(id);
        if (oldObj ==null || !Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BusinessException("必须预约中才能取消到店状态");
        }
        oldObj.setStatus(Appointment.STATUS_ARRIVAL);
        oldObj.setActualArrivalTime(new Date());
        appointmentMapper.updateByArrival(oldObj);
    }

    @Override
    public void changStatus(Long id) {
        // 必须是预约状态中才能进行取消
        Appointment oldObj = appointmentMapper.selectByPrimaryKey(id);
        if (oldObj ==null || !Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BusinessException("必须预约中才能取消");
        }
        oldObj.setStatus(Appointment.STATUS_CANCEL);
        appointmentMapper.changStatus(oldObj);
    }

    @Override
    public void changeStatus(Long appointmentId, Integer statusSettle) {
        appointmentMapper.changeStatus(appointmentId,statusSettle);
    }


}
